package itesm.coler.service;

import itesm.coler.data.CourseDAO;
import itesm.coler.data.DAOFactory;
import itesm.coler.models.Course;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Produces({"application/xml","application/json"})
@Path("/courses")
public class CourseResource {

	DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
	CourseDAO courseDAO = MySQLFactory.getCourseDAO();
	
	@GET
	public ArrayList<Course> getAllCourses(){
		try {
			ArrayList<Course> courses = courseDAO.findAll();
			return courses;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/course/{courseId}/")
	public Course getCourse(@PathParam("courseId") String courseId) {
		Course course = courseDAO.findCourseById(courseId);
		return course;
	}
	
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/professor/{professorUserId}/")
	public ArrayList<Course> getCoursesByProfessor(@PathParam("professorUserId") String professorUserId) {
		ArrayList<Course> courses = courseDAO.findAllByProfessor(professorUserId);
		return courses;
	}
	
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/student/{studentUserId}/")
	public ArrayList<Course> getCoursesByStudent(@PathParam("studentUserId") String studentUserId) {
		ArrayList<Course> courses = courseDAO.findAllByStudent(studentUserId);
		return courses;
	}
	
	@PUT
	  @Path("/add")
	  @Produces("text/plain")
	  @Consumes(MediaType.APPLICATION_JSON)
	  public Response addCourse(Course course) {
	   try {
	    courseDAO.insertCourse(course);
	    String result = "Course saved : " + course;
	    return Response.status(201).entity(result).build();
	   } catch (Exception sqle){
		   sqle.printStackTrace();
		   return null;
	   }
	  }
	
	@PUT
	@Path("/modify")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response modifyUser(Course course){
		  try {
			boolean result =courseDAO.updateCourse(course);
			System.out.println("Resultado de ModifyUser ="+result);
			return Response.status(201).entity("ok").build();	
		  } catch (Exception sqle){
			   sqle.printStackTrace();
			   return null;
		   }
	}
	
	@DELETE
	@Path("/delete/{courseId}")
	public Response deleteCourse(@PathParam("courseId") String courseId){
		courseDAO.deleteCourse(courseId);
		return Response.status(201).entity("ok").build();
	}
	
	@PUT
	@Path("/deactivate/{courseId}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response deactivateCourse(@PathParam("courseId") String courseId){
	    courseDAO.updateCourseActiveStatus(courseId, false);
		return Response.status(201).entity("ok").build();
	}
	
	@PUT
	@Path("/activate/{courseId}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response activateUser(@PathParam("courseId") String courseId){
		courseDAO.updateCourseActiveStatus(courseId, true);
		return Response.status(201).entity("ok").build();
	}
	
}
